package com.gregbugaj.reflector.test;

import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.junit.Test;

import com.gregbugaj.reflector.filter.RegexFilterStrategy;
import com.gregbugaj.reflector.renderer.HtmlRenderer;
import com.gregbugaj.reflector.renderer.TreeRenderer;
import com.gregbugaj.reflector.test.model.Info;
import com.gregbugaj.reflector.visitors.JSFDumpContext;
import com.gregbugaj.reflector.visitors.XMLVisitor;

public class FilterStrategyTester {
	Calendar cal=GregorianCalendar.getInstance();

	@Test
	public void simpleTest(){
		Info infoModel=new Info();
		infoModel.infoA="Some string with text";
		infoModel.infoB="More text B";
		infoModel.dob=new Date();

		JSFDumpContext context=null;
		XMLVisitor visitor = new XMLVisitor();		
		context = visitor.getContext();

		context.setFilterStrategy(new RegexFilterStrategy().addProperty("info(.)*"));
		context.getFilterStrategy().addProperty("infoA");


		visitor.visit( infoModel );
		FileWriter writer = null;
		InputStream stream = null;
		try {
			context.print();

			TreeRenderer renderer=new TreeRenderer();
			renderer.render(context);

			writer = new FileWriter(new File("c:\\temp_render.htm"));

			String template="/templates/template.txt";
			stream = this.getClass().getResourceAsStream(template);

			int result=-1;
			StringBuffer sb=new StringBuffer();
			while((result=stream.read())!=-1){
				sb.append((char)result);
			}

			String tempCode=sb.toString();
			tempCode = tempCode.replaceAll("\\{output\\}", renderer.getBufferToString());	
			writer.write(tempCode);
			
			close(writer);
			close(stream);
			
		} catch (Exception e) {
			e.printStackTrace();
		}			

	}
	
	private void close(Closeable resource){
		try{
			resource.close();
		}catch(Exception ex){
		}
	}
}
